﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCPostData = System.Collections.Generic.Dictionary<string, object>;
using System.Collections.Specialized;
using XinghuaPack.Utility;

namespace UCenterLib {
    public class UCClient2 {
        public static UCRegisterStatus UCUserRegister(string username, string password, string email, out int uid) {
            uid = UCClient.UCUserRegister(username, password, email);
            if (uid > 0) {
                return UCRegisterStatus.Success;
            }
            else {
                return (UCRegisterStatus)uid;
            }
        }

        public static UCRegisterStatus UCUserRegister(string username, string password, string email, int questionid, string answer, out int uid) {
            uid = UCClient.UCUserRegister(username, password, email, questionid, answer);
            if (uid > 0) {
                return UCRegisterStatus.Success;
            }
            else {
                return (UCRegisterStatus)uid;
            }
        }

        public static UCLoginStatus UCUserLogin(string username, string password, int userId, bool checkQuestion, int questionId, string answer, out int uid, out string email){
            UCPostData data = UCClient.UCUserLogin(username, password, userId, checkQuestion, questionId, answer);

            if (data != null) {
                uid = Convert.ToInt32(data["0"]);

                if (uid > 0) {
                    email = (string)data["3"];
                    return UCLoginStatus.Success;
                }
                else {
                    email = string.Empty;
                    return (UCLoginStatus)uid;
                }
            }

            email = string.Empty;
            uid = -99;
            return UCLoginStatus.UnknwonError;
        }

        public static UCLoginStatus UCUserLogin(string username, string password, int userId, bool checkQuestion, int questionId, string answer, 
            out int uid, out string email, out bool duplicated) {
            UCPostData data = UCClient.UCUserLogin(username,password, userId, checkQuestion, questionId, answer);

            if (data != null) {
                uid = Convert.ToInt32(data["0"]);
                if (uid > 0) {
                    email = (string)data["3"];
                    duplicated = (data["4"] == "1");

                    return UCLoginStatus.Success;
                }
                else {
                    email = string.Empty;
                    duplicated = false;
                    return (UCLoginStatus)uid;
                }
            }
            else {
                uid = -99;
                email = string.Empty;
                duplicated = false;
                return UCLoginStatus.UnknwonError;
            }
        }

        public static string UCGetUser(int userId, out string email) {
            UCPostData data = UCClient.UCGetUser("NONE", userId);
            if (data == null) {
                email = string.Empty;
                return string.Empty;
            } else {
                string username = Convert.ToString(data["1"]);
                email = Convert.ToString(data["2"]);

                return username;
            }
        }

        public static int UCGetUser(string username, out string email) {
            if (string.IsNullOrEmpty(username)) {
                email = string.Empty;
                return -1;
            }

            UCPostData data = UCClient.UCGetUser(username, 0);
            if (data == null) {
                email = string.Empty;
                return -1;
            } else {
                int userId = Convert.ToInt32(data["0"]);
                email = (string)data["2"];

                return userId;
            }
        }
    }

    public enum UCRegisterStatus {
        Success = 0,
        //-1:用户名不合法
        InvalidUsername = -1,
        //-2:包含不允许注册的词语
        IncludeBadWords = -2,
        //-3:用户名已经存在
        UsernameExisted = -3,
        //-4:Email 格式有误
        InvalidEmail = -4,
        //-5:Email 不允许注册
        EmailNotAllowed = -5,
        //-6:该 Email 已经被注册
        EmailExisted = -6,
        UnknwonError = -99
    }

    public enum UCLoginStatus {
        Success = 0,
        /*
         大于 0:返回用户 ID，表示用户登录成功
        -1:用户不存在，或者被删除
        -2:密码错
        -3:安全提问错
         */
        UserNotExist = -1,
        InvalidPassword = -2,
        WrongQuestion = -3,
        UnknwonError = -99
    }
}
